﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace BrsdServer
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class main_modify : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int flag=Convert.ToInt32(context.Request.QueryString["flag"]);
            dnsInfoBLL dnsInfo = new dnsInfoBLL();
            hiednsLib.DnsModifyClass hiedns = new hiednsLib.DnsModifyClass();
            //判断DNS是否存在。
            if (flag==1)
            {
                string dns = context.Request.QueryString["dns"].ToString();
                int isBind=dnsInfo.BindMac(dns);
                if (isBind==0)
                {
                    context.Response.Write(1);
                }
                else
                {
                    context.Response.Write(0);
                }
            }
            //写入DNS和DVR所在地。
            if (flag==2)
            {
                string dns = context.Request.QueryString["dns"].ToString();
                string dvrAddr = context.Server.UrlDecode(context.Request.QueryString["dvrAddr"].ToString());
                string mac = context.Request.QueryString["mac"].ToString();
                string ip=context.Request.QueryString["ip"].ToString();

                if (dnsInfo.UpdateDns(mac, dns, dvrAddr) == 1)
                {
                    string server_domain = System.Configuration.ConfigurationManager.AppSettings["domain"];
                    string server_ip = System.Configuration.ConfigurationManager.AppSettings["ip"];
                    //ret=0：表示绑定成功，否则不成功
                    hiedns.SetDnsSvr(server_domain, server_ip, 0);
                    int ret = hiedns.Hie_DnsModify(mac, dns,ip, 0);
                    if (ret==0)
                    {
                        context.Response.Write(2);
                    }   
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
